// filaname: string_utf_convert.cpp
// authro: enic
// date: 2015-05-13

#pragma once

#include "string_utf_convert.h"

#ifdef WIN32
	#include "windows.h"
//	#include "stringapiset.h"
#endif

namespace X
{

	std::string U16ToUtf8(const std::wstring strUtf16)
	{
		std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> cvt;
		std::string strUtf8 = cvt.to_bytes(strUtf16); // utf-16 to utf-8

		return strUtf8;
	}

	std::wstring U8ToUtf16(const std::string strUtf8)
	{
		std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> cvt;
		std::wstring strUtf16 = cvt.from_bytes(strUtf8); // utf-8 to utf-16

		return strUtf16;
	}


#ifdef WIN32

	std::string U16ToGbk(const std::wstring strUtf16)
	{
		// wide char to multi char
		int iTextLen = WideCharToMultiByte(CP_ACP, 0, strUtf16.c_str(), -1, NULL, 0, NULL, NULL);
		char* pElementText = new char[iTextLen + 1];
		memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
		WideCharToMultiByte(CP_ACP, 0, strUtf16.c_str(), -1, pElementText, iTextLen, NULL, NULL);
		std::string strText;
		strText = pElementText;
		delete[] pElementText;
		return strText;
	}

	std::wstring GbkToU16(const std::string strGbk)
	{
		int iTextLen = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), strGbk.size(), NULL, 0);
		std::wstring strU16;
		if (iTextLen <= 0)
		{
			return strU16;
		}

		strU16.resize(iTextLen);
		MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), strGbk.size(), const_cast<wchar_t*>(strU16.data()), iTextLen);

		return strU16;
	}

#endif

};